package com.liuceng.zen.service;

import com.liuceng.zen.lib.sms.SmsClientSend;
import com.liuceng.zen.pojo.domain.*;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MessageService {
  private final static Logger LOG = LoggerFactory.getLogger(MessageService.class);
  @Autowired
  VelocityEngine velocityEngine;
  @Autowired
  private DepartmentService departmentService;
  @Autowired
  private AdminService adminService;
  @Autowired
  private MemberOperationNotificationService memberOperationNotificationService;
  @Autowired
  private SmsSenderService smsSenderService;
  @Value("${note.url}")
  private String url;
  @Value("${note.userid}")
  private String userid;
  @Value("${note.account}")
  private String account;
  @Value("${note.password}")
  private String password;
  @Value("${note.type}")
  private String type;
  @Value("${hailiangtong.url}")
  private String hailiangtong;
  @Value("${time.delay}")
  private Long delay;

  public String doMessage(String tname, String time, String cname, String operation, Admin admin,
      Integer id) {
    Map<String, Object> model = new HashMap<>();
    model.put("tname", tname);
    model.put("time", time);
    model.put("cname", cname);
    model.put("operation", operation);
    model.put("hailiangtong", hailiangtong);
    model.put("mid", id);
    String content =
        VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "notification.vm", model);
    String send = "短信接口已经关闭";
    if (type.equals("true")) {
      send =
          SmsClientSend.sendSms(url, "send", userid, account, password, admin.getPhone(), content);
    }
    LOG.debug(send.toString());
    LOG.error(send.toString());
    return content;
  }

  public void timerTaskAdGroup(MemberOperationNotification memberOperationNotification, Admin admin,
                               Member member, FrontAdGroup f, Date date) {
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
      @Override
      public void run() {
        MemberOperationNotification notification =
            memberOperationNotificationService.find(memberOperationNotification.getId());
        if (0 == notification.getDataFlag()) {
          Admin principal = adminService.selectByDepartmentId(admin.getDepartmentId());
          memberOperationNotification.setAdminName(principal.getName());
          memberOperationNotification.setDepartmentId(principal.getDepartmentId());
          memberOperationNotification.setId(null);
          memberOperationNotificationService.insert(memberOperationNotification);
          String content = "客户"+member.getCompanyName()+"于"+simple.format(date)+"对广告："+f.getTitle()+"进行了"+memberOperationNotification.getAction()+"操作，请及时处理。收到信息请点击下面的链接进行确认："+hailiangtong+"/member_operation_notification/edit/"+memberOperationNotification.getId()+"【海量通】";
          smsSenderService.sender(principal.getPhone(),content);
          LOG.info(content);//打印短信日志
          memberOperationNotification.setContent(content);
          memberOperationNotificationService.update(memberOperationNotification);

          Timer timer = new Timer();
          TimerTask task2 = new TimerTask() {
            @Override
            public void run() {
              MemberOperationNotification notification =
                  memberOperationNotificationService.find(memberOperationNotification.getId());
              if (0 == notification.getDataFlag()) {
                Admin principal = adminService.find(1);
                memberOperationNotification.setAdminName(principal.getName());
                memberOperationNotification.setDepartmentId(principal.getDepartmentId());
                memberOperationNotification.setId(null);
                memberOperationNotificationService.insert(memberOperationNotification);
                String content = "客户"+member.getCompanyName()+"于"+simple.format(date)+"对广告："+f.getTitle()+"进行了"+memberOperationNotification.getAction()+"操作，请及时处理。收到信息请点击下面的链接进行确认："+hailiangtong+"/member_operation_notification/edit/"+memberOperationNotification.getId()+"【海量通】";
                smsSenderService.sender(principal.getPhone(),content);
                LOG.info(content);//打印短信日志
                memberOperationNotification.setContent(content);
                memberOperationNotificationService.update(memberOperationNotification);
              } else {
                cancel();
              }
            }
          };
          timer.schedule(task2, delay);
        } else {
          cancel();
        }
      }
    };
    timer.schedule(task, delay);
  }

  public void timerTaskCampaign(MemberOperationNotification memberOperationNotification,
      Admin admin, Member member, FrontCampaign frontCampaign) {
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
      @Override
      public void run() {
        MemberOperationNotification notification =
            memberOperationNotificationService.find(memberOperationNotification.getId());
        if (0 == notification.getDataFlag()) {
          Admin principal = adminService.selectByDepartmentId(admin.getDepartmentId());
          memberOperationNotification.setAdminName(principal.getName());
          memberOperationNotification.setDepartmentId(principal.getDepartmentId());
          memberOperationNotification.setId(null);
          memberOperationNotificationService.insert(memberOperationNotification);
          //String content = doMessage(member.getCompanyName(),
          //    simple.format(frontCampaign.getUpdatedAt()), memberOperationNotification.getAction(),
          //    frontCampaign.getName(), admin, memberOperationNotification.getId());
          //新短信平台
          String content = "客户"+member.getCompanyName()+"于"+simple.format(frontCampaign.getUpdatedAt())+"对广告："+frontCampaign.getName()+"进行了"+memberOperationNotification.getAction()+"操作，请及时处理。收到信息请点击下面的链接进行确认："+hailiangtong+"/member_operation_notification/edit/"+memberOperationNotification.getId()+"【海量通】";
          smsSenderService.sender(principal.getPhone(),content);
          LOG.info(content);//打印短信日志
          memberOperationNotification.setContent(content);
          memberOperationNotificationService.update(memberOperationNotification);

          Timer timer = new Timer();
          TimerTask task2 = new TimerTask() {
            @Override
            public void run() {
              MemberOperationNotification notification =
                  memberOperationNotificationService.find(memberOperationNotification.getId());
              if (0 == notification.getDataFlag()) {
                Admin principal = adminService.find(1);
                memberOperationNotification.setAdminName(principal.getName());
                memberOperationNotification.setDepartmentId(principal.getDepartmentId());
                memberOperationNotification.setId(null);
                memberOperationNotificationService.insert(memberOperationNotification);
                //String content =
                //    doMessage(member.getCompanyName(), simple.format(frontCampaign.getUpdatedAt()),
                //        memberOperationNotification.getAction(), frontCampaign.getName(), admin,
                //       memberOperationNotification.getId());
                //新短信平台
                String content = "客户"+member.getCompanyName()+"于"+simple.format(frontCampaign.getUpdatedAt())+"对广告："+frontCampaign.getName()+"进行了"+memberOperationNotification.getAction()+"操作，请及时处理。收到信息请点击下面的链接进行确认："+hailiangtong+"/member_operation_notification/edit/"+memberOperationNotification.getId()+"【海量通】";
                smsSenderService.sender(principal.getPhone(),content);
                LOG.info(content);//打印短信日志
                memberOperationNotification.setContent(content);
                memberOperationNotification.setContent(content);
                memberOperationNotificationService.update(memberOperationNotification);
              } else {
                cancel();
              }
            }
          };
          timer.schedule(task2, delay);
        } else {
          cancel();
        }
      }
    };
    timer.schedule(task, delay);
  }

  public static void main(String[] args) {
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
      @Override
      public void run() {
        System.out.println("aaaaaaaaaaaa");

        Timer timer = new Timer();
        TimerTask task2 = new TimerTask() {
          @Override
          public void run() {
            System.out.println("bbbbbbbbbbbb");
          }
        };
        timer.schedule(task2, 1000);
      }
    };
    timer.schedule(task, 1000);
  }

}
